package com.hc.daoimp;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hc.dao.HouseDao;
import com.hc.entity.House;
import com.hc.util.JDBCUtil;

public class HouseDaoImpl implements HouseDao {
	//添加
	@Override
	public void addHouse(House house) {
		JDBCUtil.sql="INSERT INTO house VALUES(NULL,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.ps.setString(1, house.getHaddress());
			JDBCUtil.ps.setString(2,house.getHcommunity());
			JDBCUtil.ps.setInt(3, house.getHunit());
			JDBCUtil.ps.setInt(4, house.getHfloor());
			JDBCUtil.ps.setInt(5, house.getHnum());
			JDBCUtil.ps.setInt(6, house.getHarea());
			JDBCUtil.ps.setString(7, house.getHtoward());
			JDBCUtil.ps.setString(8, house.getHdecorate());
			JDBCUtil.ps.setString(9, house.getHshaungqi());
			JDBCUtil.ps.setInt(10, house.getHlimit());
			JDBCUtil.ps.setString(11, house.getHfacilities());
			JDBCUtil.ps.setInt(12, house.getHprice());
			JDBCUtil.ps.setString(13, house.getHstate());
			JDBCUtil.ps.setString(14, house.getHmessage());
			JDBCUtil.ps.setString(15, house.getHaddtime());
			JDBCUtil.ps.setString(16, house.getHupdatetime());
			
			JDBCUtil.ps.executeUpdate();
			System.out.println("添加成功");
			JDBCUtil.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	//条件查询房屋信息
	@Override
	public List<House> pageSelect(String dq, String xq, int mj, int jg, String zt) {
		ArrayList<House> arrayList = new ArrayList<>();
		JDBCUtil.sql="SELECT * FROM house WHERE haddress LIKE ? AND hcommunity LIKE ? "
				+ "AND  harea LIKE ? AND hprice LIKE ? AND hstate LIKE ?";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			
			JDBCUtil.ps.setString(1, "%"+dq+"%");
			JDBCUtil.ps.setString(2, "%"+xq+"%");
			JDBCUtil.ps.setString(3, "%"+mj+"%");
			JDBCUtil.ps.setString(4, "%"+jg+"%");
			JDBCUtil.ps.setString(5, "%"+zt+"%");
			
			JDBCUtil.rs=JDBCUtil.ps.executeQuery();
			while (JDBCUtil.rs.next()) {
				House house = new House();
				house.setHid(JDBCUtil.rs.getInt(1));
				house.setHaddress(JDBCUtil.rs.getString(2));
				house.setHcommunity(JDBCUtil.rs.getString(3));
				house.setHunit(JDBCUtil.rs.getInt(4));
				house.setHfloor(JDBCUtil.rs.getInt(5));
				house.setHnum(JDBCUtil.rs.getInt(6));
				house.setHarea(JDBCUtil.rs.getInt(7));
				house.setHtoward(JDBCUtil.rs.getString(8));
				house.setHdecorate(JDBCUtil.rs.getString(9));
				house.setHshaungqi(JDBCUtil.rs.getString(10));
				house.setHlimit(JDBCUtil.rs.getInt(11));
				house.setHfacilities(JDBCUtil.rs.getString(12));
				house.setHprice(JDBCUtil.rs.getInt(13));
				house.setHstate(JDBCUtil.rs.getString(14));
				house.setHmessage(JDBCUtil.rs.getString(15));
				house.setHaddtime(JDBCUtil.rs.getString(16));
				house.setHupdatetime(JDBCUtil.rs.getString(17));
				
				arrayList.add(house);
				/*JDBCUtil.close();*/
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return arrayList;
	}
	@Override
	public House getDetails(int id) {
		House house = new House();
		JDBCUtil.sql="SELECT * FROM house WHERE hid=?";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			
			JDBCUtil.ps.setInt(1, id);
			JDBCUtil.rs=JDBCUtil.ps.executeQuery();
			while (JDBCUtil.rs.next()) {
				house.setHid(JDBCUtil.rs.getInt(1));
				house.setHaddress(JDBCUtil.rs.getString(2));
				house.setHcommunity(JDBCUtil.rs.getString(3));
				house.setHunit(JDBCUtil.rs.getInt(4));
				house.setHfloor(JDBCUtil.rs.getInt(5));
				house.setHnum(JDBCUtil.rs.getInt(6));
				house.setHarea(JDBCUtil.rs.getInt(7));
				house.setHtoward(JDBCUtil.rs.getString(8));
				house.setHdecorate(JDBCUtil.rs.getString(9));
				house.setHshaungqi(JDBCUtil.rs.getString(10));
				house.setHlimit(JDBCUtil.rs.getInt(11));
				house.setHfacilities(JDBCUtil.rs.getString(12));
				house.setHprice(JDBCUtil.rs.getInt(13));
				house.setHstate(JDBCUtil.rs.getString(14));
				house.setHmessage(JDBCUtil.rs.getString(15));
				house.setHaddtime(JDBCUtil.rs.getString(16));
				house.setHupdatetime(JDBCUtil.rs.getString(17));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return house;
	}
	//更新
	@Override
	public void update(House house) {
		JDBCUtil.sql="UPDATE house SET haddress=?,hcommunity=?, hunit=?"
				+ "hfloor=?, hnum=?, harea=?,htoward=?,hdecorate=?,"
				+ "hshaungqi=?,hlimit=?,hfacilities=?,hprice=?,hstate=?"
				+ "hmessage=?,haddtime=?,hupdatetime=? where hid=?";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.ps.setString(1, house.getHaddress());
			JDBCUtil.ps.setString(2, house.getHcommunity());
			JDBCUtil.ps.setInt(3, house.getHunit());
			JDBCUtil.ps.setInt(4, house.getHfloor());
			JDBCUtil.ps.setInt(5, house.getHnum());
			JDBCUtil.ps.setInt(6, house.getHarea());
			JDBCUtil.ps.setString(7, house.getHtoward());
			JDBCUtil.ps.setString(8, house.getHdecorate());
			JDBCUtil.ps.setString(9, house.getHshaungqi());
			JDBCUtil.ps.setInt(10, house.getHlimit());
			JDBCUtil.ps.setString(11, house.getHfacilities());
			JDBCUtil.ps.setInt(12, house.getHprice());
			JDBCUtil.ps.setString(13, house.getHstate());
			JDBCUtil.ps.setString(14, house.getHmessage());
			JDBCUtil.ps.setString(15, house.getHaddtime());
			JDBCUtil.ps.setString(16, house.getHupdatetime());
			JDBCUtil.ps.setInt(17, house.getHid());
			
			JDBCUtil.ps.executeUpdate();
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
				
		
	}
	@Override
	public List<House> show() {
		ArrayList<House> arrayList = new ArrayList<>();
		JDBCUtil.sql="SELECT * FROM house";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.rs=JDBCUtil.ps.executeQuery();
			
			while (JDBCUtil.rs.next()) {
				House house = new House();
				house.setHid(JDBCUtil.rs.getInt(1));
				house.setHaddress(JDBCUtil.rs.getString(2));
				house.setHcommunity(JDBCUtil.rs.getString(3));
				house.setHunit(JDBCUtil.rs.getInt(4));
				house.setHfloor(JDBCUtil.rs.getInt(5));
				house.setHnum(JDBCUtil.rs.getInt(6));
				house.setHarea(JDBCUtil.rs.getInt(7));
				house.setHtoward(JDBCUtil.rs.getString(8));
				house.setHdecorate(JDBCUtil.rs.getString(9));
				house.setHshaungqi(JDBCUtil.rs.getString(10));
				house.setHlimit(JDBCUtil.rs.getInt(11));
				house.setHfacilities(JDBCUtil.rs.getString(12));
				house.setHprice(JDBCUtil.rs.getInt(13));
				house.setHstate(JDBCUtil.rs.getString(14));
				house.setHmessage(JDBCUtil.rs.getString(15));
				house.setHaddtime(JDBCUtil.rs.getString(16));
				house.setHupdatetime(JDBCUtil.rs.getString(17));
				
				arrayList.add(house);
				/*JDBCUtil.close();*/
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return arrayList;
	}
	@Override
	public void delete(int id) {
		JDBCUtil.sql="DELETE FROM house WHERE hid=?";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.ps.setInt(1, id);
			JDBCUtil.ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

	

	

}
